package demo;

import java.util.Stack;

    public class test99 {
        public static void main(String[] args) {
            String parentheses = "()()((()))";

            boolean isValid = checkValidParentheses(parentheses);
            System.out.println(isValid);
        }

        private static boolean checkValidParentheses(String parentheses) {
            Stack<Character> stack = new Stack<>();

            for (int i = 0; i < parentheses.length(); i++) {
                char ch = parentheses.charAt(i);

                if (ch == '(') {
                    stack.push(ch);
                } else if (ch == ')') {
                    if (stack.isEmpty() || stack.peek() != '(') {
                        return false;
                    }

                    stack.pop();
                }
            }

            return stack.isEmpty();
        }
    }

